package java6681.gui;

import java6681.dao.Student6681;
import java6681.dao.Student6681Dao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.List;


public class EditStudent6681Controller {

    @FXML
    private BorderPane root;

    @FXML
    private TableView<Student6681> tvStudents;

    @FXML
    private TableColumn<Student6681, String> colSno;

    @FXML
    private TableColumn<Student6681, String> colSname;

    @FXML
    private TableColumn<Student6681, Integer> colLx1;

    @FXML
    private TableColumn<Student6681, Integer> colLx2;

    private final ObservableList<Student6681> STUDENTS = FXCollections.observableArrayList();

    private Stage stage;

    public static boolean isYes;

    @FXML
    void initialize(){
        tvStudents.setItems(STUDENTS);
        colSno.setCellValueFactory(new PropertyValueFactory<>("sno"));
        colSname.setCellValueFactory(new PropertyValueFactory<>("sname"));
        colLx1.setCellValueFactory(new PropertyValueFactory<>("lx1"));
        colLx2.setCellValueFactory(new PropertyValueFactory<>("lx2"));
        List<Student6681> allStu = Student6681Dao.findAll6681();
        if(allStu == null){return;}
        STUDENTS.addAll(allStu);
    }

    @FXML
    void add(ActionEvent event) {
        Dialog6681Controller controller = createDialog6681();
        controller.initAdd6681();
        stage.showAndWait();
        Student6681 student = controller.getStudent();
        if(isYes){
            if(Student6681Dao.add6681(student) > 0){
                showAlert("添加成功 ✔", Alert.AlertType.INFORMATION, "成功消息");
                STUDENTS.add(student);
            }else{
                showAlert("添加失败 ❌", Alert.AlertType.ERROR, "失败消息");
            }
        }
    }

    @FXML
    void change(ActionEvent event) {
        showModify();
    }

    @FXML
    void delete(ActionEvent event) {
        Student6681 selectedItem = tvStudents.getSelectionModel().getSelectedItem();
        if(selectedItem != null && Student6681Dao.delete6681(selectedItem.getId()) > 0){
            STUDENTS.remove(selectedItem);
            showAlert("删除成功!", Alert.AlertType.INFORMATION, "消息提示");
        }else{
            showAlert("请选择正确的要删除的学生 -_-||", Alert.AlertType.ERROR, "错误");
        }
    }

    @FXML
    void doubleClick(MouseEvent event) {
        if(event.getClickCount() >= 2){
            showModify();
        }
    }

    void showModify(){
        Student6681 selectedItem = tvStudents.getSelectionModel().getSelectedItem();
        if(selectedItem != null){
            Dialog6681Controller controller = createDialog6681();
            controller.initChange6681(selectedItem);
            stage.showAndWait();
            if(isYes){
                if(Student6681Dao.update6681(selectedItem.getId(), controller.getStudent()) > 0){
                    showAlert("修改成功 ✔", Alert.AlertType.INFORMATION, "成功消息");
                    tvStudents.refresh();
                }else{
                    showAlert("修改失败 ❌", Alert.AlertType.ERROR, "失败消息");
                }
            }
        }
    }

    Dialog6681Controller createDialog6681(){
        stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initOwner(root.getScene().getWindow());
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Dialog6681View.fxml"));
        try {
            stage.setScene(new Scene(loader.load()));
        } catch (IOException e) {
            showAlert("找不到节点树文件", Alert.AlertType.ERROR, "错误");
        }
        return loader.getController();
    }

    public static void showAlert(String msg, Alert.AlertType type, String title) {
        Alert alert = new Alert(type == null ? Alert.AlertType.WARNING : type, msg, ButtonType.OK);
        alert.setHeaderText("-==- 系统消息 -==-");
        alert.setTitle(title == null ? "注意" : title);
        alert.show();
    }

}